package org.apache.cordova;

import java.io.Serializable;

/**
 * Local storage definition and singleton pattern access.
 * @author Cyril Joui
 */
public abstract class LocalStorage implements Serializable {

	/**
	 * UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Singleton pattern.
	 */
	private static LocalStorage instance;

	/**
	 * @return singleton instance
	 */
	public static LocalStorage getInstance() {
		synchronized (LocalStorage.class) {
			if (instance == null) {
				instance = new LocalStorageImpl();
			}
		}
		return instance;
	}

	/**
	 * Release instance.
	 */
	public static void releaseInstance() {
		if (instance != null) {
			instance.release();
			instance = null;
		}
	}

	/**
	 * Release instance.
	 */
	abstract protected void release();

	/**
	 * Get item's value from its key.
	 * @param key item key
	 * @return item value
	 */
	abstract public String getItem(String key);

	/**
	 * Set value to item.
	 * @param key item key
	 * @param value value to set
	 */
	abstract public void setItem(String key, String value);

	/**
	 * Remove given item.
	 * @param key item's key
	 */
	abstract public void removeItem(String key);

	/**
	 * Remove everything on storage.
	 */
	abstract public void clear();
}
